﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicaNegocio;
using Entidades;
using AccesoBD;
using System.Web.Security;

namespace CMS
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //Response.Redirect("iniciosi.aspx");
            string usr, password;
            try
            {
                usr = txtUsuario.Text.Trim();
                password = txtPassword.Text.Trim();
                if (GestorUsuariosWeb.validarUsuarioWeb(usr, password))
                {
                    Session["idUserConectado"] = UsuariosWeb.ObtenerUsuario(usr);
                    string rol = GestorUsuariosWeb.obtenerRolUsuarioWeb(usr).Nombre + "|";
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, usr, DateTime.Now, DateTime.Now.AddMinutes(60), false, rol);
                    string encrTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie cookie = new HttpCookie("medisys", encrTicket);
                    Response.Cookies.Add(cookie);
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(usr, false));
                }
                else
                {
                    lblError.Text = "Datos inválidos de acceso. Vuelva a intentarlo";
                    txtPassword.Text = "";
                    txtUsuario.Text = "";
                }
            }
            catch (Exception ex)
            {
                lblError.Text = "Error al intentar iniciar sesión";
                txtPassword.Text = "";
                txtUsuario.Text = "";
            }
        }
    }
}